# import smtplib
# from email.mime.text import MIMEText
# from email.header import Header
# from_addr = '2232152882@qq.com'
# to_addr = '2022566488@qq.com'
# password = 'cudqydvgjtctdiid'
# host = 'smtp.qq.com'
# port = 465
# html_file = """
# <a href = "https://poster.taptap.cn/r/NGMI7ciULGX9.html">月之二大战猎月人</a>
# """
# message = MIMEText(html_file, 'html', 'utf-8')
# message['From'] = Header(from_addr)
# message['To'] = Header(to_addr)
# message['Subject'] = Header('原神五周年快乐', 'utf-8')
# try:
#     smtpObj = smtplib.SMTP_SSL(host, port)
#     smtpObj.login(from_addr, password)
#     smtpObj.sendmail(from_addr, to_addr, message.as_string())
#     smtpObj.quit()
#     print('邮件终于发送成功了')
# except Exception as m:
#     print('邮件还是发送失败了',m)
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
class Email:
    def __init__(self,from_addr,to_addr,password,msg,msg_header,host = 'smtp.qq.com',port=465):
        self.from_addr = from_addr
        self.to_addr = to_addr
        self.password = password
        self.msg = msg
        self.msg_header = msg_header
        self.host = host
        self.port = port
    def send_text(self):
        message = MIMEText(self.msg,'plain','utf-8')
        message['From'] = Header(self.from_addr)
        message['To'] = Header(self.to_addr)
        message['Subject'] = Header(self.msg_header)
        try:
            smtp = smtplib.SMTP_SSL(self.host,self.port)
            smtp.login(self.from_addr,self.password)
            smtp.sendmail(self.from_addr,self.to_addr,self.msg.as_string())
            smtp.quit()
            print('邮件发送成功')
        except Exception as s:
            print('邮件发送失败',s)
    def send_html(self):
        message = MIMEText(self.msg,'html','utf-8')
        message['From'] = Header(self.from_addr)
        message['To'] = Header(self.to_addr)
        message['Subject'] = Header(self.msg_header)
        try:
            smtp = smtplib.SMTP_SSL(self.host,self.port)
            smtp.login(self.from_addr,self.password)
            smtp.sendmail(self.from_addr,self.to_addr,self.msg.as_string())
            smtp.quit()
            print('邮件发送成功')
        except Exception as s:
            print('邮件发送失败',s)
    def send_and(self):
        message = MIMEMultipart()
        html_file = """
        <h2>附件邮件</h2>
        """
        m1 = MIMEText(html_file, 'html', 'utf-8')
        message.attach(m1)
        m2 = MIMEText(open(self.msg, 'rb').read(), 'base64', 'utf-8')
        m2["Content-Type"] = 'application/octet-stream'
        m2["Content-Disposition"] = 'attachment; filename="附件"'
        message.attach(m2)
        message['From'] = Header(self.from_addr)
        message['To'] = Header(self.to_addr)
        message['Subject'] = Header(self.msg_header)
        try:
            smtp = smtplib.SMTP_SSL(self.host,self.port)
            smtp.login(self.from_addr,self.password)
            smtp.sendmail(self.from_addr,self.to_addr,message.as_string())
            smtp.quit()
            print('邮件发送成功')
        except Exception as s:
            print('邮件发送失败',s)
sbs = Email('2232152882@qq.com','2022566488@qq.com','cudqydvgjtctdiid','mysql密码和安装位置.txt','头信息')
sbs.send_and()









